package com.xianshiyue;

import cn.jpush.api.push.PushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.List;

/**
 * @author dfshi
 */
public class PushApiManager {
    private static final String appKey = "f2b068812e8d339d9ba4b2e8";
    private static final String masterSecret = "aca4cf6fbaa513ac814aa3ac";

    private static final int maxRetryTimes = 2;

    private static PushClient pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);

    //True 表示推送生产环境，False 表示要推送开发环境；
    private static final boolean flag = true;

    //广播所有人都能收到
    public static boolean pushMessageToAll(String content) {

        boolean opRet = true;
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all()).setOptions(
                        Options.newBuilder().setApnsProduction(flag).build())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setBadge(1)
                                .setAlert(content)
                                .setSound("happy.caf")
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .build())
                        .build())
                .build();
        try {
            PushApiManager.pushClient.sendPush(pushPayload);
        } catch (Exception e) {
            opRet = false;
            e.printStackTrace();
            System.out.println("sendPushMessageByAlias exception:" + e.getLocalizedMessage());
        }
        return opRet;
    }

    public static boolean pushMessageForZd(List<String> alias, String content) {

        boolean opRet = true;
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias)).setOptions(
                        Options.newBuilder().setApnsProduction(flag).build())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setBadge(1)
                                .setAlert(content)
                                .setSound("happy.caf")
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .build())
                        .build())
                .build();
        try {
            PushApiManager.pushClient.sendPush(pushPayload);
        } catch (Exception e) {
            opRet = false;
            e.printStackTrace();
            System.out.println("sendPushMessageByAlias exception:" + e.getLocalizedMessage());
        }
        return opRet;
    }

    public static void main(String[] arg) {

		pushMessageToAll("tttttttttttttttttttt");
    }

}
